package engine.display.newAttack.action
{
	import engine.display.newAttack.AttackLogic;
	import engine.display.newAttack.AttackPlayer;
	import engine.display.newAttack.action.effect.PointPlayLite;
	import engine.display.newAttack.vo.AttackActionVo;
	import engine.display.newAttack.vo.DamageVo;
	import engine.display.newAttack.vo.FlowVo;
	import engine.flag.PlayerActionFlag;
	import engine.logic.McPool;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/**
	 *	单人近战物理 
	 * @author lw
	 * 
	 */
	public class SinglePhyCloseAttack extends PhyCloseAttack
	{
		/**
		 *标示当前是否是反击 
		 */		
		private var _isCounter:Boolean=false;
		public function SinglePhyCloseAttack(fPlayer:AttackPlayer, 
											 tPlayers:Array, 
											 flowVo : FlowVo, 
											 attackActionVo:AttackActionVo,
											 onPlay:Function, 
											 onPlayOver:Function=null, 
											 soundHit:String="")
		{
			super(fPlayer, tPlayers, flowVo, attackActionVo, onPlay, 
				onPlayOver, soundHit );
		}
		
		override protected function onPlayAttack(e:Event):void
		{
			super.onPlayAttack( e );
			
			var	fromSkin:MovieClip	=	_fPlayer.getSkin();
			
			if(fromSkin == null)
			{
				attackToRun();
				_fPlayer.removeEventListener(Event.ENTER_FRAME,onPlayAttack);
				return ;
			}
			if(!_isEnd && ( fromSkin.currentLabel == PlayerActionFlag.NormalAttack 
				|| fromSkin.currentLabel == PlayerActionFlag.MagicAttack ))
			{
				_fPlayer.y = _tPlayer.y - 1;
				onRunning();
				//玩家向上一像素，让受伤特效压住攻击者
				
				if( isCrit() )
				{
					shake( AttackLogic.getInstance().attackScene );
				}
				
//				if(_tPlayer.dead == 0)
//				{
				var damageVo : DamageVo = _damageVos.shift();
				if(damageVo) _tPlayer.damage( damageVo );
				
				if(_attackActionVo.beAttactEffectSkin != null && damageVo.result != 5 )
				{
					var mc : MovieClip = McPool.borrowRes( _attackActionVo.beAttactEffectSkin )
					
					if(_fPlayer.team == 0)
					{
						mc.scaleX	=	1;
					}
					else
					{
						mc.scaleX	=	-1;
					}
					new PointPlayLite(_tPlayer.effectPlace,//_attackActionVo.beAttactEffectData["skin"]
						_tPlayer[ _attackActionVo.beAttactEffectPos+ "Point" ],
						mc );
				}
				
				_isEnd	=	true;
				
				if(!_isCounter)//如果是反击 那么不管buff设置
				{
					_fPlayer.showBuffer( _flowVo.buffer);
				}
				if(!damageVo.counterattack)
				{
					_fPlayer.resetMp(_flowVo.mp);
				}
				
				//发送命中声音
				//ModelLocator.getInstance().dispatchEvent(new SoundEvent(_soundHit));
			}else if( _isEnd && fromSkin.currentLabel == PlayerActionFlag.Wait )
			{
				_fPlayer.removeEventListener(Event.ENTER_FRAME,onPlayAttack);
				
				if( _damageVos.length > 0 && _damageVos[ 0 ].counterattack )
				{//反击回合
					counterattack();
					return;
				}
				attackToRun();
			}
		}
		
		protected function isShake():Boolean
		{
			return _attackActionVo.skillAttackType == 5 && isCrit();
		}
		
		protected function isCrit():Boolean
		{
			return _damageVos[ 0 ].result == 2;
		}
		
		/**
		 *	反击 
		 */		
		protected function counterattack():void
		{
			_isEnd = false;
			_isCounter=true;
			
			var tempPlayer : AttackPlayer = _fPlayer;
			
			_fPlayer = _tPlayer;
			_tPlayer = tempPlayer;
			_fPlayer.addEventListener(Event.ENTER_FRAME,onPlayAttack);
			_fPlayer.playToTarget( 3 );
		}
	}
}